package com.ajun.controller;

import com.ajun.api.facade.PaymentFeignClient;
import com.ajun.common.entity.R;
import com.ajun.entity.Payment;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Order Controller
 *
 * @author AJun
 * @date 2021-07-11
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    // private static final String PAYMENT_API_URL = "http://localhost:8001/api";
    private static final String PAYMENT_API_URL = "http://CLOUD-PROVIDER-PAYMENT/api";

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private PaymentFeignClient paymentFeignClient;

    @GetMapping("/discovery")
    public R<JSONObject> getPort() {
        log.info("远程调用 payment 服务,url: {}", PAYMENT_API_URL + "/payment/discovery");
        // return restTemplate.getForObject(PAYMENT_API_URL + "/payment/discovery", R.class);
        return paymentFeignClient.discovery();
    }

    @PostMapping("/create")
    public R<Boolean> createPayment(@RequestBody Payment payment) {
        log.info("远程调用 payment 服务,url: {}", PAYMENT_API_URL + "/payment/create");
        // return restTemplate.postForObject(PAYMENT_API_URL + "/payment/create", payment, R.class);
        return paymentFeignClient.createPayment(payment);
    }

    @GetMapping("/get/{id}")
    public R<Payment> getPayment(@PathVariable("id") Long id) {
        log.info("远程调用 payment 服务,url: {}", PAYMENT_API_URL + "/payment/get/" + id);
        // return restTemplate.getForObject(PAYMENT_API_URL + "/payment/get/" + id, R.class);
        return paymentFeignClient.getPayment(id);
    }
}
